/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.aop;

import java.util.concurrent.atomic.AtomicBoolean;

import com.hyts.aop.error.AopException;
import com.hyts.aop.facade.AopCglibExecutor;
import com.hyts.aop.facade.AopJdkExecutor;
import com.hyts.aop.handler.cglib.CglibAopHandler;
import com.hyts.aop.proxy.AopContext;
import com.hyts.aop.proxy.ProxyBuilder;

/** 
 * @title AopExecutor.java  
 * @package com.hyts.aop  
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年4月25日 
 * @version V1.0 
 */
public final class AopProxy implements AopJdkExecutor, AopCglibExecutor {

    /**
     * @fieldName isInit
     * @fieldType AtomicBoolean
     */ 
    private static AtomicBoolean isInit = new AtomicBoolean(false);

    /**
     * aop应用服务上下文
     */
    private static AopContext globalAopContext = null;
    
    /**
     * @param clazz
     * @return
     * @exception
     */ 
    public static <T> T getCallerProxy(Class<T> clazz){
        //全局AOPContext上下文
        if(globalAopContext == null) {
            throw new AopException("没有初始化AopContext上下文，请调用initAopContext方法");
        }
        return AopContext.getInvokeProxyObject(clazz);
    }

    /**  
     * @param backPackage
     * @exception
     */ 
    public AopContext initAopContext(String backPackage) {
        if(isInit.compareAndSet(false, true)) {
            globalAopContext = new AopContext(backPackage);
            globalAopContext.init();
        }
        return globalAopContext;
    }

    public AopContext getGlobalAopContext(){
        return globalAopContext;
    }

}
